package com.luyidog.swaggerdemo.config;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@EnableSwagger2  //开启Swagger2
@Configuration //加入到配置类 springboot声明配置类
public class SwaggerConfig {

    //配置了Swagger的Docket的Bean实例
    //enable(false) 是否启动Swagger。默认启动
    @Bean
    public Docket docket(Environment environment) {
        //Q:在生产环境中使用swagger，在发布的时候不使用。
        //获取当前项目的生产环境
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev", "test");
        //通过environment.acceptsProfiles()判断是否处在自己设定的环境
        final boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("Naughty")//配置默认分组
                .enable(flag)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.luyidog.swaggerdemo.controller"))
                //.paths(PathSelectors.ant("/luyidog/**"))
                .build();//build新建某个东西=>扫描接口

    }


    //配置Swagger信息 => apiInfo()
    private ApiInfo apiInfo() {
        //作者信息
        final Contact contact = new Contact("Naughty", "https://blog.csdn.net/g200000", "luyidog@aliyun.com");

        return new ApiInfo("Naughty的SwaggerDemoAPI文档",
                "年轻就应该投资大脑",
                "v1.0",
                "https://blog.csdn.net/g200000",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
